package leetcode.leetcode938;

import java.util.Stack;

/**
 * @author czt
 * @version 1.0
 * @since 2025/1/2
 */
public class Leetcode938 {

    public int rangeSumBST(TreeNode root, int low, int high) {
        TreeNode node = root;
        Stack<TreeNode> stack = new Stack<>();
        int sum = 0;
        while(node != null || !stack.isEmpty()){
            if(node != null){
                stack.push(node);
                node = node.left;
            }else{
                TreeNode pop = stack.pop();
                if(pop.val >= low && pop.val <= high){
                    sum += pop.val;

                }
                node = pop.right;

            }
        }
        return sum;

    }
}
